#include "dswmessagesession.h"

static const ST_P2PMSG_T m_p2pmsg[] = {

	{"MSG_PUA_GET_PARAM_REQ", "MSG_PUA_GET_PARAM_RESP", CDswmessagesession::cbGetParamReq},
	{"MSG_PUA_SET_PARAM_REQ","MSG_PUA_SET_PARAM_RESP", CDswmessagesession::cbSetParamReq},
	{"MSG_PUA_GET_FACTORY_PARAM_REQ","MSG_PUA_GET_FACTORY_PARAM_RESP", CDswmessagesession::cbGetFactoryParamReq},
	{"MSG_PUA_SET_FACTORY_PARAM_REQ","MSG_PUA_SET_FACTORY_PARAM_RESP", CDswmessagesession::cbSetFactoryParamReq},

	{"MSG_SD_DIRLIST_REQ", "MSG_SD_DIRLIST_RESP", CDswmessagesession::cbGetSdDirListReq},

	{"MSG_DOWN_REC_VIDEO_REQ","MSG_DOWN_REC_VIDEO_RESP", CDswmessagesession::cbGetSdRecFnDownReq},

	{"MSG_GET_REC_VIDEO_REQ", "MSG_GET_REC_VIDEO_RESP", CDswmessagesession::cbGetSdRecFnPlayReq},
	{"MSG_STOP_REC_VIDEO_REQ", "MSG_STOP_REC_VIDEO_RESP", CDswmessagesession::cbProcessStopRecVideoReq},
	{"MSG_SET_REC_VIDEO_SPD_REQ", "MSG_SET_REC_VIDEO_SPD_RESP", CDswmessagesession::cbProcessSetRecVideoSpdReq},
	{"MSG_SET_REC_VIDEO_POS_REQ", "MSG_SET_REC_VIDEO_POS_RESP", CDswmessagesession::cbProcessSetRecVideoPosReq},
	{"END","", NULL},
};

CDswmessagesession::CDswmessagesession()
{
}

CDswmessagesession::~CDswmessagesession()
{
	LOG4CPLUS_DEBUG(log_dsw, "Destroy message session!");
}

/*CDswmessagesession* CDswmessagesession::createNew(LPST_ENV_T pEnv, struct event_base *base, int nfd, int nHeadSize, char* pKey)
{
	struct timeval tv = {15, 0};
	struct bufferevent *pBev = bufferevent_socket_new(base, nfd, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
	bufferevent_set_timeouts(pBev, &tv, NULL);
	return new CDswmessagesession(pEnv, pBev, nHeadSize, pKey);
}*/

CDswmessagesession::CDswmessagesession(LPST_ENV_T pEnv, struct bufferevent * pBev, int nHeadSize, char* pKey)
:  CTcpcombase(pEnv, pBev, nHeadSize)
{
	LOG4CPLUS_DEBUG(log_dsw, "Create message session!");
/*	CMarkup xml;
	
	strcpy(m_sKey, pKey);
	MakeReportPUA(xml);
	SendMsg((char*)xml.GetDoc().c_str());
*/
}

/*
void CDswmessagesession::SendMsg(char * pMsg)
{
	char sBuffer[1024 * 5];
	*((int*)sBuffer) = strlen(pMsg);
	memcpy(sBuffer + sizeof(int), pMsg, strlen(pMsg));
	int nMsgLen = strlen(pMsg) + sizeof(int);
	evbuffer_add(bufferevent_get_output(m_pBev), sBuffer, nMsgLen);
}
*/
/*
void CDswmessagesession::MakeReportPUA(CMarkup & xml)
{
	char sip[64], sbuf[32], sid[32];
	int ch_num, nNum;
	void* pParam;
	
	xml.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
	xml.AddElem("Message");
	xml.AddAttrib("Version", "1.0.0");
	xml.AddChildElem("DSW_HEADER");
	xml.IntoElem();
		xml.AddChildElem("MessageType", "MSG_PUA_REGIST");
	xml.OutOfElem();
	xml.AddChildElem("DSW_KEY", defDSWKEY);
	m_pEnv->pOnNotifyFunc(PMSG_GET_BASIC, pParam);
	xml.AddChildElem("DSW_PUA_ID", ((LPST_DEVICE_BASE)pParam)->devid);
	pParam = (void*)sid;
	m_pEnv->pOnNotifyFunc(PMSG_GET_DEVTYPE, pParam);
	xml.AddChildElem("PUA_TYPE", *((int*)pParam));
	xml.AddChildElem("DSW_PUA_SN", "");
	pParam = (void*)sip;
	m_pEnv->pOnNotifyFunc(PMSG_GET_ACTIVEIP, pParam);
	if (strlen(sip) > 0){
		xml.AddChildElem("DSW_PUA_IP", sip);
		xml.AddChildElem("DSW_PUA_PORT", 80);
		m_pEnv->pOnNotifyFunc(PMSG_GET_NETINFO, pParam);
		xml.AddChildElem("DSW_PUA_MAC", ((LPST_LAN_PARAM)pParam)->mac);
		m_pEnv->pOnNotifyFunc(PMSG_GET_LOCATE, pParam);
		xml.AddChildElem("DSW_PUA_LOCATE", ((LPST_LOCATION_PARAM)pParam)->dev_location);
		m_pEnv->pOnNotifyFunc(PMSG_GET_ALARMINFO, pParam);
		xml.AddChildElem("DSW_PUA_LOCK", ((LPST_ALARM_PARAM)pParam)->enalarm);
		m_pEnv->pOnNotifyFunc(PMSG_GET_OSVER, pParam);
		xml.AddChildElem("DSW_OSVER", (char*)pParam);
		m_pEnv->pOnNotifyFunc(PMSG_GET_APPVER, pParam);
		xml.AddChildElem("DSW_APPVER", (char*)pParam);
		pParam = (void*)sip;
		m_pEnv->pOnNotifyFunc(PMSG_GET_VIDEOLISTEN_PORT, pParam);
		xml.AddChildElem("DSW_VPORT", *((int*)pParam));
		xml.AddChildElem("DSW_CHN", MAX_CHANNEL);
		for (ch_num = 0; ch_num < MAX_CHANNEL; ch_num++){
			sprintf(sbuf, "CH%d", ch_num);
			xml.AddChildElem(sbuf);
			xml.IntoElem();
				xml.AddChildElem("DSW_STREAM", "1;0");
				nNum = ch_num;
				pParam = (void*)&nNum;
				m_pEnv->pOnNotifyFunc(PMSG_GET_VIDEOINFO, pParam);
				xml.AddChildElem("LOCATE", ((LPST_CH_PARAM)pParam)->ch_location);
			xml.OutOfElem();
		}
		xml.AddChildElem("DSW_RESULT");
		xml.IntoElem();
			xml.AddChildElem("Errcode", 0);
		xml.OutOfElem();
	}
}
*/
bool CDswmessagesession::ProcessMessage(char* pMsg, int nLen, struct evbuffer *outbuf)
{
	CMarkup xml;

	xml.SetDoc(pMsg);
	if(!xml.FindElem("Message"))
		return false;
	xml.IntoElem();
	if(!xml.FindElem("DSW_HEADER"))
		return false;
	xml.IntoElem();
	if(!xml.FindElem("MessageType"))
		return false;
	char smsgreq[64];
	smsgreq[0] = '\0';
	strcpy(smsgreq, xml.GetData().c_str());
	xml.OutOfElem();
	LPST_P2PMSG_T pPos = (LPST_P2PMSG_T)m_p2pmsg;
	while(strcmp(pPos->sMsgReq, "END") != 0){
		if ((strcmp(pPos->sMsgReq, smsgreq) == 0) && (pPos->pP2PFunc != NULL)){
			pPos->pP2PFunc(pMsg, pPos->sMsgResp, this);
			return true;
			break;
		}
		pPos ++;
	}
	return false;
}
/*
void CDswmessagesession::MakeRespXML(const char* pMsgType, CMarkup & xml, int nerrcode)
{
	void* pParam;
	xml.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	xml.AddElem("Message");
	xml.AddAttrib("Version", "1.0.0");
	xml.AddChildElem("DSW_HEADER");
	xml.IntoElem();
		xml.AddChildElem("MessageType", pMsgType);
	xml.OutOfElem();
	xml.AddChildElem("DSW_KEY", m_sKey);
	m_pEnv->pOnNotifyFunc(PMSG_GET_BASIC, pParam);
	xml.AddChildElem("DSW_PUA_ID", ((LPST_DEVICE_BASE)pParam)->devid);
	xml.AddChildElem("DSW_PUA_SN", "");
	xml.AddChildElem("DSW_RESULT");
	xml.IntoElem();
	xml.AddChildElem("Errcode", nerrcode);
	xml.OutOfElem();
}
*/
void CDswmessagesession::GetNetWifiParam(CMarkup& xml)
{
	void* pParam;
	m_pEnv->pOnNotifyFunc(PMSG_GET_WIFIINFO, pParam);
	xml.AddElem("WIFI");
	xml.IntoElem();
	{
		xml.AddElem("APMAC", ((LPST_WIFI_PARAM)pParam)->apmac);
		xml.AddElem("SSID", ((LPST_WIFI_PARAM)pParam)->ssid);
		xml.AddElem("SECURITY", ((LPST_WIFI_PARAM)pParam)->securitytype);
		xml.AddElem("PASSW", ((LPST_WIFI_PARAM)pParam)->password);
		xml.AddElem("NETTYPE", ((LPST_WIFI_PARAM)pParam)->nettype);
		xml.AddElem("IP", ((LPST_WIFI_PARAM)pParam)->ip);
		xml.AddElem("MASK", ((LPST_WIFI_PARAM)pParam)->mask);
		xml.AddElem("GW", ((LPST_WIFI_PARAM)pParam)->gw);
	}
	xml.OutOfElem();
}

void CDswmessagesession::GetNetEthParam(CMarkup& xml)
{
	void* pParam;
	m_pEnv->pOnNotifyFunc(PMSG_GET_NETINFO, pParam);
	xml.AddElem("ETH");
	xml.IntoElem();
	{
		xml.AddElem("TYPE", ((LPST_LAN_PARAM)pParam)->nettype);
		xml.AddElem("IP", ((LPST_LAN_PARAM)pParam)->ip);
		xml.AddElem("MASK", ((LPST_LAN_PARAM)pParam)->mask);
		xml.AddElem("MAC", ((LPST_LAN_PARAM)pParam)->mac);
		xml.AddElem("GATE", ((LPST_LAN_PARAM)pParam)->gw);
		xml.AddElem("DNSTYPE", ((LPST_LAN_PARAM)pParam)->dnstype);
		xml.AddElem("MDNS", ((LPST_LAN_PARAM)pParam)->dns1);
		xml.AddElem("SDNS", ((LPST_LAN_PARAM)pParam)->dns2);
	}
	xml.OutOfElem();
}

void CDswmessagesession::GetNetPppoeParam(CMarkup& xml)
{
	void* pParam;
	m_pEnv->pOnNotifyFunc(PMSG_GET_PPPINFO, pParam);
	xml.AddElem("PPPOE");
	xml.IntoElem();
	{
		xml.AddElem("ENPPPOE", ((LPST_PPPOE_PARAM)pParam)->enpppoe);
		xml.AddElem("USER", ((LPST_PPPOE_PARAM)pParam)->user);
		xml.AddElem("PASSW", ((LPST_PPPOE_PARAM)pParam)->password);
	}
	xml.OutOfElem();
}

void CDswmessagesession::GetAudioParam(CMarkup& xml)
{
	void* pParam;
	int nCh = 0;
	pParam = (void*)&nCh;
	m_pEnv->pOnNotifyFunc(PMSG_GET_VIDEOINFO, pParam);
	xml.AddElem("DEV_MEDIA");
	xml.IntoElem();
	xml.AddElem("VIDEO");
	xml.IntoElem();
	xml.AddElem("TYPE", ((LPST_CH_PARAM)pParam)->vtype);
	xml.OutOfElem();
	xml.AddElem("AUDIO");
	xml.IntoElem();
	m_pEnv->pOnNotifyFunc(PMSG_GET_AUDIOINFO, pParam);
	xml.AddElem("CHANNEL", ((LPST_AUDIO_PARAM)pParam)->stereo);
	xml.AddElem("TYPE", ((LPST_AUDIO_PARAM)pParam)->type);
	xml.AddElem("RATE", ((LPST_AUDIO_PARAM)pParam)->speed);
	xml.AddElem("SIZE", ((LPST_AUDIO_PARAM)pParam)->size);
	xml.OutOfElem();
	xml.OutOfElem();
}

void CDswmessagesession::GetVideoParam(CMarkup& xml, int ch_num)
{
	void *pParam;
	int sub_num, nCh;
	
	nCh = ch_num;
	pParam = (void*)&nCh;
	m_pEnv->pOnNotifyFunc(PMSG_GET_VIDEOINFO, pParam);

	xml.AddElem("CHN_MEDIA");
	xml.IntoElem();
	xml.AddElem("CHN", ch_num);
	xml.AddElem("VIDEO");
	xml.IntoElem();
	for (sub_num = 0; sub_num < MAX_V_BITSTREAM_NUM; sub_num++){
		xml.AddElem("SUBSTRM");
		xml.IntoElem();
		if (sub_num == MAIN_STREAM)
			xml.AddElem("STREAM", MAIN_STREAM_CMD);
		else if (sub_num == SUB_STREAM)
			xml.AddElem("STREAM", SUB_STREAM_CMD);
		else
			xml.AddElem("STREAM", sub_num);
		xml.AddElem("SIZE", ((LPST_CH_PARAM)pParam)->bs[sub_num].size);
		xml.AddElem("FPS", ((LPST_CH_PARAM)pParam)->bs[sub_num].fps);
		xml.AddElem("BITS", ((LPST_CH_PARAM)pParam)->bs[sub_num].bps);
		xml.AddElem("GOP", ((LPST_CH_PARAM)pParam)->bs[sub_num].gop);
		xml.AddElem("QUALITY", ((LPST_CH_PARAM)pParam)->bs[sub_num].quality);
		xml.OutOfElem();
	}
	xml.OutOfElem();
	xml.AddElem("OSD");
	xml.IntoElem();
	xml.AddElem("EN_OSD", ((LPST_CH_PARAM)pParam)->osd.enosd);
	xml.AddElem("TEXT", ((LPST_CH_PARAM)pParam)->osd.text);
	xml.OutOfElem();
	xml.AddElem("CHN_LOCATION", ((LPST_CH_PARAM)pParam)->ch_location);
	xml.OutOfElem();
}

void CDswmessagesession::GetSystemParam(CMarkup& xml)
{
	void* pParam;
	m_pEnv->pOnNotifyFunc(PMSG_GET_BASIC, pParam);
	xml.AddElem("BASIC");	
	xml.AddChildElem("PUAID", ((LPST_DEVICE_BASE)pParam)->devid);
	xml.AddChildElem("PUASN", "012345678");
	xml.AddChildElem("PUATYPE", ((LPST_DEVICE_BASE)pParam)->type);
	xml.AddChildElem("CENTER_SVR", ((LPST_DEVICE_BASE)pParam)->center);
	xml.AddChildElem("CENTER_PORT", ((LPST_DEVICE_BASE)pParam)->port);
	xml.AddChildElem("TRANS_PORT", 17600);
	xml.AddChildElem("STUN_SVR", ((LPST_DEVICE_BASE)pParam)->center);
	xml.AddChildElem("STUN_PORT", 3479);
	
}

void CDswmessagesession::GetAlarmParam(CMarkup& xml)
{
	void* pParam;
	m_pEnv->pOnNotifyFunc(PMSG_GET_ALARMINFO, pParam);
	xml.AddElem("ALARM");
	xml.IntoElem();
	xml.AddElem("EN_ALARM", ((LPST_ALARM_PARAM)pParam)->enalarm);
	xml.AddElem("PRE_VIDEO_TIME", ((LPST_ALARM_PARAM)pParam)->prevideo);
	xml.AddElem("VIDEO_TIME", ((LPST_ALARM_PARAM)pParam)->rectime);
	xml.OutOfElem();
	
}

void CDswmessagesession::GetAreCfgParam(CMarkup& xml)
{
	int ncount;
	void* pParam;
	m_pEnv->pOnNotifyFunc(PMSG_GET_ARECFG, pParam);
	xml.AddElem("ARE_CONFIG");
	xml.IntoElem();
	xml.AddElem("ARE_COUNT", ((LPST_ARE_PARAM)pParam)->count);
	for (ncount = 0; ncount < ((LPST_ARE_PARAM)pParam)->count; ncount++){
		xml.AddElem("ARE");
		xml.IntoElem();
		xml.AddElem("ID", ((LPST_ARE_PARAM)pParam)->pare[ncount].id);
		xml.AddElem("NAME", ((LPST_ARE_PARAM)pParam)->pare[ncount].name);
		xml.AddElem("ACTION", ((LPST_ARE_PARAM)pParam)->pare[ncount].action);
		xml.AddElem("TM", ((LPST_ARE_PARAM)pParam)->pare[ncount].delay);
		xml.AddElem("BINDV", ((LPST_ARE_PARAM)pParam)->pare[ncount].bindv);
		xml.AddElem("TYPE", ((LPST_ARE_PARAM)pParam)->pare[ncount].aretype);
		xml.OutOfElem();
	}
	xml.OutOfElem();

}

void CDswmessagesession::GetLocateParam(CMarkup& xml)
{
	void* pParam;
	m_pEnv->pOnNotifyFunc(PMSG_GET_LOCATE, pParam);
	xml.AddElem("LOCATION");
	xml.IntoElem();
	xml.AddElem("DEV_LOCATION",((LPST_LOCATION_PARAM)pParam)->dev_location);
	xml.OutOfElem();
}

void CDswmessagesession::GetVideoSetParam(CMarkup& xml, int ch_num)
{
	void* pParam;
	m_pEnv->pOnNotifyFunc(PMSG_GET_VIDEOPARAMINFO, pParam);
	xml.AddElem("VIDEOSET");
	xml.IntoElem();
	xml.AddElem("VIDEO");
	xml.IntoElem();
	xml.AddElem("CHN", ch_num);
	xml.AddElem("RESET", 1);
	xml.AddElem("C", ((LPST_CH_VIDEOPARAM)pParam)->vparam[ch_num].contrast);
	xml.AddElem("B", ((LPST_CH_VIDEOPARAM)pParam)->vparam[ch_num].brightness);
	xml.AddElem("H", ((LPST_CH_VIDEOPARAM)pParam)->vparam[ch_num].hue);
	xml.AddElem("S", ((LPST_CH_VIDEOPARAM)pParam)->vparam[ch_num].saturation);
	xml.OutOfElem();
	xml.OutOfElem();		
}

void CDswmessagesession::GetParamReq(void* pMsgReq, const char* pMsgResp)
{
	int ch_num, sub_num, ncount;
	char sbuf[32];

	CMarkup xmlReq;
	xmlReq.SetDoc((char*)pMsgReq);
	CMarkup* pXmlReq = &xmlReq;
	CMarkup xmlResp;

	if(pXmlReq->FindElem("Message"))
		pXmlReq->IntoElem();
	xmlResp.SetDoc("");
	MakeRespXML(pMsgResp, xmlResp, 0);
	pXmlReq->ResetMainPos();
	if (pXmlReq->FindElem("NET")){
		xmlResp.IntoElem();
		xmlResp.AddElem("NET");
		{
			xmlResp.IntoElem();
			CMarkup xmlsub;
			pXmlReq->ResetChildPos();
			if (pXmlReq->FindChildElem("WIFI") && (0!=pXmlReq->GetChildData().c_str())){
				xmlsub.SetDoc("");
				GetNetWifiParam(xmlsub);
				xmlResp.AddSubDoc( xmlsub.GetDoc() );
			}
			pXmlReq->ResetChildPos();
			if (pXmlReq->FindChildElem("ETH") && (0!=pXmlReq->GetChildData().c_str())){
				xmlsub.SetDoc("");
				GetNetEthParam(xmlsub);
				xmlResp.AddSubDoc( xmlsub.GetDoc() );
			}
			
			pXmlReq->ResetChildPos();
			if (pXmlReq->FindChildElem("PPPOE") && (0!=pXmlReq->GetChildData().c_str())){
				xmlsub.SetDoc("");
				GetNetPppoeParam(xmlsub);
				xmlResp.AddSubDoc( xmlsub.GetDoc() );
			}
			xmlResp.OutOfElem();
		}
		xmlResp.OutOfElem();
	}
	pXmlReq->ResetMainPos();
	if (pXmlReq->FindElem("MEDIA")){
		xmlResp.IntoElem();
		xmlResp.AddElem("MEDIA");
		{
			xmlResp.IntoElem();
			CMarkup xmlsub;	
			pXmlReq->ResetChildPos();
			if (pXmlReq->FindChildElem("DEV_MEDIA") ){
				xmlsub.SetDoc("");
				GetAudioParam(xmlsub);
				xmlResp.AddSubDoc( xmlsub.GetDoc() );
			}
			pXmlReq->ResetChildPos();
			if (pXmlReq->FindChildElem("CHN_MEDIA")){
				xmlsub.SetDoc("");
				pXmlReq->IntoElem();
				pXmlReq->FindChildElem("CHN");
				int ch = atoi(pXmlReq->GetChildData().c_str());
				GetVideoParam(xmlsub, ch);
				pXmlReq->OutOfElem();
				xmlResp.AddSubDoc( xmlsub.GetDoc() );
			}
			xmlResp.OutOfElem();
		}
		xmlResp.OutOfElem();
	}
	pXmlReq->ResetMainPos();
	if (pXmlReq->FindElem("SYSTEM")){
		xmlResp.IntoElem();
		xmlResp.AddElem("SYSTEM");
		{
			xmlResp.IntoElem();
			CMarkup xmlsub;
			pXmlReq->ResetChildPos();
			if (pXmlReq->FindChildElem("BASIC") && (0!=pXmlReq->GetChildData().c_str())){
				xmlsub.SetDoc("");
				GetSystemParam(xmlsub);
				xmlResp.AddSubDoc( xmlsub.GetDoc() );
			}
			pXmlReq->ResetChildPos();
			if (pXmlReq->FindChildElem("ALARM") && (0!=pXmlReq->GetChildData().c_str())){
				xmlsub.SetDoc("");
				GetAlarmParam(xmlsub);
				xmlResp.AddSubDoc( xmlsub.GetDoc() );
			}
			pXmlReq->ResetChildPos();
			if (pXmlReq->FindChildElem("ARE_CONFIG") && (0!=pXmlReq->GetChildData().c_str())){
				xmlsub.SetDoc("");
				GetAreCfgParam(xmlsub);
				xmlResp.AddSubDoc( xmlsub.GetDoc() );
			}
			pXmlReq->ResetChildPos();
			if (pXmlReq->FindChildElem("LOCATION") && (0!=pXmlReq->GetChildData().c_str())){
				xmlsub.SetDoc("");
				GetLocateParam(xmlsub);
				xmlResp.AddSubDoc( xmlsub.GetDoc() );
			}
			pXmlReq->ResetChildPos();
			xmlResp.OutOfElem();
		}
		xmlResp.OutOfElem();
	}
	pXmlReq->ResetMainPos();
	if (pXmlReq->FindElem("VIDEOSET")){
		xmlResp.IntoElem();
		if (pXmlReq->FindChildElem("VIDEO") && (0!=pXmlReq->GetChildData().c_str())){
			CMarkup xmlsub;
			xmlsub.SetDoc("");
			pXmlReq->IntoElem();
			pXmlReq->FindChildElem("CHN");
			int ch = atoi(pXmlReq->GetChildData().c_str());
			GetVideoSetParam(xmlsub, ch);
			pXmlReq->OutOfElem();
			pXmlReq->OutOfElem();
			xmlResp.AddSubDoc( xmlsub.GetDoc() );
		}
	}
	SendMsg((char*)xmlResp.GetDoc().c_str());
}

void CDswmessagesession::SetWifiParam(void* pMsg)
{
	CMarkup xml;
	xml.SetDoc((const char*)pMsg);
	CMarkup* pxml=&xml;

	ST_WIFI_PARAM nWifi;
	memset(&nWifi, 0, sizeof(nWifi));
	pxml->IntoElem();
	pxml->FindElem("APMAC");
	strcpy(nWifi.apmac, pxml->GetData().c_str());
	pxml->FindElem("SSID");
	strcpy(nWifi.ssid, pxml->GetData().c_str());
	pxml->FindElem("SECURITY");
	nWifi.securitytype = (EN_SECURITY_TYPE)atoi(pxml->GetData().c_str());
	pxml->FindElem("PASSW");
	strcpy(nWifi.password, pxml->GetData().c_str());
	pxml->FindElem("NETTYPE");
	nWifi.nettype = (EN_NET_TYPE)atoi(pxml->GetData().c_str());
	pxml->FindElem("IP");
	strcpy(nWifi.ip, pxml->GetData().c_str());
	pxml->FindElem("MASK");
	strcpy(nWifi.mask, pxml->GetData().c_str());
	pxml->FindElem("GW");
	strcpy(nWifi.gw, pxml->GetData().c_str());

	UN_PARAM_T nUnParam;
	memcpy(&(nUnParam.nLanParam), &nWifi , sizeof(ST_LAN_PARAM));
	void* pParam = (void*)&nUnParam;
	m_pEnv->pOnNotifyFunc(PMSG_SET_WIFIINFO, pParam);
}

void CDswmessagesession::SetEthParam(void* pMsg)
{
	CMarkup xml;
	ST_LAN_PARAM nEth;
	void* pParam;
	
	memset(&nEth, 0, sizeof(nEth));
	xml.SetDoc((const char*)pMsg);
	xml.FindElem("ETH");
	xml.IntoElem();
	xml.FindElem("TYPE");
	nEth.nettype = (EN_NET_TYPE)atoi(xml.GetData().c_str());
	xml.FindElem("IP");
	strcpy(nEth.ip, xml.GetData().c_str());
	xml.FindElem("MASK");
	strcpy(nEth.mask, xml.GetData().c_str());
	xml.FindElem("MAC");
	m_pEnv->pOnNotifyFunc(PMSG_GET_NETINFO, pParam);
	strcpy(nEth.mac, ((LPST_LAN_PARAM)pParam)->mac);
	xml.FindElem("GATE");
	strcpy(nEth.gw, xml.GetData().c_str());
	xml.FindElem("DNSTYPE");
	nEth.dnstype = (EN_DNS_TYPE)atoi(xml.GetData().c_str());
	xml.FindElem("MDNS");
	strcpy(nEth.dns1, xml.GetData().c_str());
	xml.FindElem("SDNS");
	strcpy(nEth.dns2, xml.GetData().c_str());

	UN_PARAM_T nUnParam;
	memcpy(&(nUnParam.nLanParam),&nEth , sizeof(ST_LAN_PARAM));
	pParam = (void*)&nUnParam;
	m_pEnv->pOnNotifyFunc(PMSG_SET_NETINFO, pParam);
}

void CDswmessagesession::SetPppoeParam(void* pMsg)
{
	CMarkup xml;
	ST_PPPOE_PARAM nEth;
	memset(&nEth, 0, sizeof(nEth));
	xml.SetDoc((const char*)pMsg);
	xml.FindElem("PPPOE");
	xml.IntoElem();
	xml.FindElem("USER");
	strcpy(nEth.user, xml.GetData().c_str());
	xml.FindElem("PASSW");
	strcpy(nEth.password, xml.GetData().c_str());

	UN_PARAM_T nUnParam;
	memcpy(&(nUnParam.nPppoeParam),&nEth , sizeof(ST_PPPOE_PARAM));
	void* pParam = (void*)&nUnParam;
	m_pEnv->pOnNotifyFunc(PMSG_SET_PPPINFO, pParam);
}

void CDswmessagesession::SetNetParam(void* pMsg)
{
	CMarkup* pxml=(CMarkup*)pMsg;
	if (pxml->FindElem("NET")){
		if (pxml->FindChildElem("WIFI")){
			pxml->IntoElem();
			SetWifiParam((char*)pxml->GetSubDoc().c_str());
			pxml->OutOfElem();
		}
		pxml->ResetChildPos();
		if (pxml->FindChildElem("ETH")){
			pxml->IntoElem();
			SetEthParam((char*)pxml->GetSubDoc().c_str());
			pxml->OutOfElem();
		}
		pxml->ResetChildPos();
		if (pxml->FindChildElem("PPPOE")){
			pxml->IntoElem();
			SetPppoeParam((char*)pxml->GetSubDoc().c_str());
			pxml->OutOfElem();
		}
	}
}

void CDswmessagesession::SetVideoParam(void* pMsg)
{
	int sub_num;
	int p2pch_type;
	CMarkup xml;
	ST_CH_PARAM nChParam;
	memset(&nChParam, 0, sizeof(nChParam));
	xml.SetDoc((const char*)pMsg);
	xml.FindElem("MEDIA");
	xml.IntoElem();
	xml.FindElem("DEV_MEDIA");
	xml.IntoElem();
	xml.FindElem("VIDEO");
	xml.IntoElem();
	xml.FindElem("TYPE");
	p2pch_type = atoi(xml.GetData().c_str());
	xml.OutOfElem();
	xml.OutOfElem();
	xml.FindElem("CHN_MEDIA");
	xml.IntoElem();
	xml.FindElem("CHN");
	int ch = atoi(xml.GetData().c_str());
	nChParam.ch_num=ch;
	nChParam.vtype = (EN_VIDEO_TYPE)p2pch_type;
	xml.FindElem("VIDEO");
	xml.IntoElem();
	for (sub_num = 0; sub_num < MAX_V_BITSTREAM_NUM; sub_num++){
		xml.FindElem("SUBSTRM");
		xml.IntoElem();
		xml.FindElem("SIZE");
		nChParam.bs[sub_num].size = (EN_VIDEO_SIZE_TYPE)atoi(xml.GetData().c_str());
		xml.FindElem("FPS");
		nChParam.bs[sub_num].fps = atoi(xml.GetData().c_str());
		xml.FindElem("BITS");
		nChParam.bs[sub_num].bps = atoi(xml.GetData().c_str());
		xml.FindElem("GOP");
		nChParam.bs[sub_num].gop = atoi(xml.GetData().c_str());
		xml.FindElem("QUALITY");
		nChParam.bs[sub_num].quality = atoi(xml.GetData().c_str());
		xml.OutOfElem();
	}
	xml.OutOfElem();
	xml.FindElem("OSD");
	xml.IntoElem();
	xml.FindElem("EN_OSD");
	nChParam.osd.enosd = atoi(xml.GetData().c_str());
	xml.FindElem("TEXT");
	strcpy(nChParam.osd.text, xml.GetData().c_str());
	xml.OutOfElem();
	xml.FindElem("CHN_LOCATION");
	strcpy(nChParam.ch_location, xml.GetData().c_str());
	xml.OutOfElem();

	UN_PARAM_T nUnParam;
	memcpy(&(nUnParam.nChParam),&nChParam , sizeof(ST_CH_PARAM));
	void* pParam = (void*)&nUnParam;
	m_pEnv->pOnNotifyFunc(PMSG_SET_VIDEOINFO, pParam);
}

void CDswmessagesession::SetAudioParam(void* pMsg)
{
	CMarkup xml;

	xml.SetDoc((const char*)pMsg);
	ST_AUDIO_PARAM nAudio;
	memset(&nAudio, 0, sizeof(nAudio));
	xml.FindElem("DEV_MEDIA");
	xml.IntoElem();
	xml.FindElem("AUDIO");
	xml.IntoElem();
	xml.FindElem("CHANNEL");
	nAudio.stereo = atoi(xml.GetData().c_str());
	xml.FindElem("TYPE");
	nAudio.type = (EN_AUDIO_TYPE)atoi(xml.GetData().c_str());
	xml.FindElem("RATE");
	nAudio.speed = (EN_SAMPLE_SPEED_TYPE)atoi(xml.GetData().c_str());
	xml.FindElem("SIZE");
	nAudio.size = (EN_SAMPLE_BITS_TYPE)atoi(xml.GetData().c_str());
	xml.OutOfElem();
	xml.OutOfElem();
	UN_PARAM_T nUnParam;
	memcpy(&(nUnParam.nAudioParam),&nAudio , sizeof(ST_AUDIO_PARAM));
	void* pParam = (void*)&nUnParam;
	m_pEnv->pOnNotifyFunc(PMSG_SET_AUDIOINFO, pParam);	
}

void CDswmessagesession::SetMediaParam(void* pMsg)
{
	CMarkup* pxml=(CMarkup*)pMsg;
	if (pxml->FindElem("MEDIA")){
		if (pxml->FindChildElem("CHN_MEDIA"))
			SetVideoParam((char*)pxml->GetSubDoc().c_str());
		else if (pxml->FindChildElem("DEV_MEDIA")){
			pxml->IntoElem();
			SetAudioParam((char*)pxml->GetSubDoc().c_str());
			pxml->OutOfElem();
		}
	}
}

void CDswmessagesession::SetAlarmParam(void* pMsg)
{
	CMarkup xml;
	ST_ALARM_PARAM nAlarm;
	void* pParam;
	memset(&nAlarm, 0, sizeof(nAlarm));
	xml.SetDoc((const char*)pMsg);
	xml.FindElem("ALARM");
	xml.IntoElem();
	xml.FindElem("EN_ALARM");
	nAlarm.enalarm = atoi(xml.GetData().c_str());
	pParam = (void*)&nAlarm.enalarm;
	m_pEnv->pOnNotifyFunc(PMSG_SETGARRISONWITHDRAW, pParam);
	xml.FindElem("PRE_VIDEO_TIME");
	nAlarm.prevideo = atoi(xml.GetData().c_str());
	xml.FindElem("VIDEO_TIME");
	nAlarm.rectime = atoi(xml.GetData().c_str());
	xml.OutOfElem();
	UN_PARAM_T nUnParam;
	memcpy(&(nUnParam.nAlarm),&nAlarm , sizeof(ST_ALARM_PARAM));
	pParam = (void*)&nUnParam;
	m_pEnv->pOnNotifyFunc(PMSG_SET_ALARMINFO, pParam);
}

void CDswmessagesession::SetAreCfgParam(void* pMsg)
{
	int ncount;
	CMarkup xml;
	ST_ARE_PARAM nAre;
	memset(&nAre, 0, sizeof(nAre));
	xml.SetDoc((const char*)pMsg);
	xml.FindElem("ARE_CONFIG");
	xml.IntoElem();
	xml.FindElem("ARE_COUNT");
	nAre.count = atoi(xml.GetData().c_str());
	if (nAre.count > 0){
		nAre.pare = new ST_ARE_T[nAre.count];
		for (ncount = 0; ncount < nAre.count; ncount++){
			xml.FindElem("ARE");
			xml.IntoElem();
			xml.FindElem("ID");
			strcpy(nAre.pare[ncount].id, xml.GetData().c_str());
			xml.FindElem("NAME");
			strcpy(nAre.pare[ncount].name, xml.GetData().c_str());
			xml.FindElem("ACTION");
			nAre.pare[ncount].action = atoi(xml.GetData().c_str());
			xml.FindElem("TM");
			nAre.pare[ncount].delay = atoi(xml.GetData().c_str());
			xml.FindElem("BINDV");
			nAre.pare[ncount].bindv = atol(xml.GetData().c_str());
			xml.FindElem("TYPE");
			nAre.pare[ncount].aretype = (EN_ARE_TYPE)atoi(xml.GetData().c_str());
			xml.OutOfElem();
		}
	}
	xml.OutOfElem();
	UN_PARAM_T nUnParam;
	memcpy(&(nUnParam.nAre), &nAre , sizeof(ST_ARE_PARAM));
	void* pParam = (void*)&nUnParam;
	m_pEnv->pOnNotifyFunc(PMSG_SET_ARECFG, pParam);
	if (nAre.count != 0)
		delete nAre.pare;
}

void CDswmessagesession::SetOtherParam(void* pMsg)
{
	CMarkup xml;
	xml.SetDoc((const char*)pMsg);
	xml.FindElem("OTHER");
	xml.IntoElem();
	xml.FindElem("REBOOT");
	int reboot = atoi(xml.GetData().c_str());
	xml.OutOfElem();
	if(reboot){
		void* pParam;
		m_pEnv->pOnNotifyFunc(PMSG_SET_REBOOT, pParam);
	}
}

void CDswmessagesession::SetSystemParam(void* pMsg)
{
	CMarkup* pxml=(CMarkup*)pMsg;
	if (pxml->FindElem("SYSTEM")){
		if (pxml->FindChildElem("ALARM")){
			pxml->IntoElem();
			SetAlarmParam((char*)pxml->GetSubDoc().c_str());
			pxml->OutOfElem();
		}
		pxml->ResetChildPos();
		if (pxml->FindChildElem("ARE_CONFIG")){
			pxml->IntoElem();
			SetAreCfgParam((char*)pxml->GetSubDoc().c_str());
			pxml->OutOfElem();
		}
		if (pxml->FindChildElem("OTHER")){
			pxml->IntoElem();
			SetOtherParam((char*)pxml->GetSubDoc().c_str());
			pxml->OutOfElem();
		}
	}
}

void CDswmessagesession::SetLocateParam(void* pMsg)
{
	CMarkup* pxml=(CMarkup*)pMsg;
	if (pxml->FindElem("LOCATE")){
		if (pxml->FindChildElem("DEV_LOCATE")){
			pxml->IntoElem();
			CMarkup xml;
			ST_LOCATION_PARAM nLocation;
			memset(&nLocation, 0, sizeof(nLocation));
			xml.SetDoc((char*)pxml->GetSubDoc().c_str());
			xml.FindElem("DEV_LOCATE");
			xml.IntoElem();
			xml.FindElem("LOCATION");
			strcpy(nLocation.dev_location,xml.GetData().c_str());
			xml.OutOfElem();
			UN_PARAM_T nUnParam;
			memcpy(&(nUnParam.nLocation), &nLocation , sizeof(ST_LOCATION_PARAM));
			void* pParam = (void*)&nUnParam;
			m_pEnv->pOnNotifyFunc(PMSG_SET_LOCATE, pParam);
			pxml->OutOfElem();
		}
	}

}

void CDswmessagesession::SetVideoSetParam(void* pMsg)
{
	CMarkup* pxml=(CMarkup*)pMsg;
	if (pxml->FindElem("VIDEOSET")){
		if (pxml->FindChildElem("VIDEO")){
			pxml->IntoElem();
			CMarkup xml;
			ST_CH_VIDEOPARAM nChVideoParam;
			void* pParam;
			m_pEnv->pOnNotifyFunc(PMSG_GET_VIDEOPARAMINFO, pParam);
			memset(&nChVideoParam, 0, sizeof(nChVideoParam));
			memcpy(&nChVideoParam, (LPST_CH_VIDEOPARAM)pParam, sizeof(ST_CH_VIDEOPARAM));
			xml.SetDoc((char*)pxml->GetSubDoc().c_str());
			xml.FindElem("VIDEOSET");
			xml.IntoElem();
			xml.FindElem("VIDEO");
			xml.IntoElem();
			xml.FindElem("CHN");
			int dev_ch_param = atoi(xml.GetData().c_str());
			nChVideoParam.ch_num = dev_ch_param;
			xml.FindElem("RESET");
			int resetvalue=atoi(xml.GetData().c_str());
			if (resetvalue == 0){
				nChVideoParam.vparam[dev_ch_param].contrast = defContrast;
				nChVideoParam.vparam[dev_ch_param].brightness = defBrightness;
				nChVideoParam.vparam[dev_ch_param].hue = defHue;
				nChVideoParam.vparam[dev_ch_param].saturation =defSaturation;
			}
			else{
				xml.FindElem("C");
				nChVideoParam.vparam[dev_ch_param].contrast = atoi(xml.GetData().c_str());
				xml.FindElem("B");
				nChVideoParam.vparam[dev_ch_param].brightness = atoi(xml.GetData().c_str());
				xml.FindElem("H");
				nChVideoParam.vparam[dev_ch_param].hue = atoi(xml.GetData().c_str());	
				xml.FindElem("S");
				nChVideoParam.vparam[dev_ch_param].saturation = atoi(xml.GetData().c_str());
			}
			xml.OutOfElem();
			UN_PARAM_T nUnParam;
			memcpy(&(nUnParam.nChVideoParam), &nChVideoParam , sizeof(ST_CH_VIDEOPARAM));
			pParam = (void*)&nUnParam;
			m_pEnv->pOnNotifyFunc(PMSG_SET_VIDEOPARAMINFO, pParam);
			pxml->OutOfElem();
		}
		}
}

void CDswmessagesession::DealSetParamReq(void * pMsgReq, const char* pMsgResp)
{
	CMarkup xml;
	xml.SetDoc((const char*)pMsgReq);
	xml.FindElem("Message");
	xml.IntoElem();
	xml.ResetMainPos();
	SetNetParam(&xml);
	SetMediaParam(&xml);
	SetSystemParam(&xml);
	SetLocateParam(&xml);
	SetVideoSetParam(&xml);
	xml.SetDoc("");
	MakeRespXML(pMsgResp, xml, 0);
	SendMsg((char*)xml.GetDoc().c_str());
}

void CDswmessagesession::GetFactoryParamReq(void* pMsgReq, const char* pMsgResp)
{
	void* pParam;

	CMarkup xml;
	xml.SetDoc("");
	MakeRespXML(pMsgResp, xml, 0);
	xml.AddChildElem("FACTORY_PARAM");
	xml.AddChildAttrib("CODE", 0);
	xml.IntoElem();
		m_pEnv->pOnNotifyFunc(PMSG_GET_NETINFO, pParam);
		xml.AddChildElem("ETHMAC", ((LPST_LAN_PARAM)pParam)->mac);
	m_pEnv->pOnNotifyFunc(PMSG_GET_BASIC, pParam);
		xml.AddChildElem("PUAID", ((LPST_DEVICE_BASE)pParam)->devid);
		xml.AddChildElem("PUASN", "012345678");
		int ncount;
		xml.AddChildElem("ARE_CONFIG");
			xml.IntoElem();
				m_pEnv->pOnNotifyFunc(PMSG_GET_ARECFG, pParam);
				xml.AddChildElem("ARE_COUNT", ((LPST_ARE_PARAM)pParam)->count);
				for (ncount = 0; ncount < ((LPST_ARE_PARAM)pParam)->count; ncount++){
					xml.AddChildElem("ARE");
					xml.IntoElem();
						xml.AddChildElem("ID", ((LPST_ARE_PARAM)pParam)->pare[ncount].id);
						xml.AddChildElem("NAME", ((LPST_ARE_PARAM)pParam)->pare[ncount].name);
						xml.AddChildElem("ACTION", ((LPST_ARE_PARAM)pParam)->pare[ncount].action);
						xml.AddChildElem("TM", ((LPST_ARE_PARAM)pParam)->pare[ncount].delay);
						xml.AddChildElem("BINDV", ((LPST_ARE_PARAM)pParam)->pare[ncount].bindv);
						xml.AddChildElem("TYPE", ((LPST_ARE_PARAM)pParam)->pare[ncount].aretype);
					xml.OutOfElem();
				}
			xml.OutOfElem();
	xml.OutOfElem();
	SendMsg((char*)xml.GetDoc().c_str());
}

void CDswmessagesession::SetFactoryParamReq(void * pMsg, const char* pMsgResp)
{
	int ncount;
	ST_FACTORY nFactory;
	memset(&nFactory, 0, sizeof(nFactory));
	CMarkup xml;
	xml.SetDoc((const char*)pMsg);
	xml.FindChildElem("FACTORY_PARAM");
	nFactory.ncode = atoi(xml.GetChildAttrib("CODE").c_str());
		xml.IntoElem();
		xml.FindChildElem("ETHMAC");
		sprintf(nFactory.ethmac, "%s", xml.GetChildData().c_str());
		xml.FindChildElem("PUAID");
		sprintf(nFactory.spuaid, "%s", xml.GetChildData().c_str());
		xml.FindChildElem("ARE_CONFIG");
		xml.IntoElem();
		xml.FindChildElem("ARE_COUNT");
		nFactory.nAre.count = atoi(xml.GetChildData().c_str());
		nFactory.nAre.pare = new ST_ARE_T[nFactory.nAre.count];
		for (ncount = 0; ncount < nFactory.nAre.count; ncount++)
		{
			xml.FindChildElem("ARE");
			xml.IntoElem();
			xml.FindChildElem("ID");
			strcpy(nFactory.nAre.pare[ncount].id, xml.GetChildData().c_str());
			xml.FindChildElem("NAME");
			strcpy(nFactory.nAre.pare[ncount].name, xml.GetChildData().c_str());
			xml.FindChildElem("ACTION");
			nFactory.nAre.pare[ncount].action = atoi(xml.GetChildData().c_str());
			xml.FindChildElem("TM");
			nFactory.nAre.pare[ncount].delay = atoi(xml.GetChildData().c_str());
			xml.FindChildElem("BINDV");
			nFactory.nAre.pare[ncount].bindv = atol(xml.GetChildData().c_str());
			xml.FindChildElem("TYPE");
			nFactory.nAre.pare[ncount].aretype = (EN_ARE_TYPE)atoi(xml.GetChildData().c_str());
			xml.OutOfElem();
		}
		xml.OutOfElem();
		xml.OutOfElem();

	UN_PARAM_T nUnParam;
	memcpy(&(nUnParam.nFactory),&nFactory , sizeof(ST_FACTORY));
	void* pParam = (void*)&nUnParam;
	m_pEnv->pOnNotifyFunc(PMSG_SET_FACTORY_PARAM, pParam);

	xml.SetDoc("");
	MakeRespXML(pMsgResp, xml, 0);
	SendMsg((char*)xml.GetDoc().c_str());
}

int CDswmessagesession::GetDirFile(string & sDirList, char *pDir, int nIndex, int nPageSize, int &nRestCount, bool &bIsDir)
{
	if (strlen(pDir) <= 0){
		bIsDir = true;
		sprintf(pDir, "%s/%s", MOUNT_PT, MOUNT_PT_HD);
	}
	else{
		char* pChDir = pDir + strlen(pDir) - 4;
		if ((strcmp(pChDir, "ch00") == 0) || (strcmp(pChDir, "ch01") == 0) || (strcmp(pChDir, "ch02") == 0) || (strcmp(pChDir, "ch03") == 0))
			bIsDir = false;
		else
			bIsDir = true;
	}
	
	struct dirent **pDirList;
	char* pCurDir = pDir;
	void* pParam = (void*)pCurDir;
	int nCount = m_pEnv->pOnNotifyFunc(PMSG_GET_DIRFILE, pParam);
	int nRetCount = 0;
	pDirList = (struct dirent **)pParam;
	if (nCount <= 0){
		nRestCount = 0;
	}
	else{
		int nMaxId = nCount - nIndex;
		CMarkup xml;
		xml.SetDoc("");
		while(nCount-- > 0){
			if ((nCount <= nMaxId) && (nPageSize > 0)){
				xml.AddElem("DIR", pDirList[nCount]->d_name);
				nRetCount++;
				nPageSize--;
			}
			else
				nRestCount++;
			free(pDirList[nCount]);
		}
		sDirList = xml.GetDoc();
	}
	return nRetCount;
}

void CDswmessagesession::GetSdDirListReq(void* pMsgReq, const char* pMsgResp)
{
	char sCurDir[256];
	int nPageSize, nIndex;
	int nListCount = 0;
	int nRestCount = 0;
	int errcode = -1;
	string sDirList = string("");
	CMarkup xml;
	bool bIsDir;

	xml.SetDoc((const char*)pMsgReq);
	if (!xml.FindChildElem("CUR_DIR"))
		goto END_GETSDDIRLIST;
	strcpy(sCurDir, xml.GetChildData().c_str());
	if (!xml.FindChildElem("INDEX"))
		goto END_GETSDDIRLIST;
	nIndex = atoi(xml.GetChildData().c_str());
	if (!xml.FindChildElem("PAGESIZE"))
		goto END_GETSDDIRLIST;
	nPageSize = atoi(xml.GetChildData().c_str());

	nListCount = GetDirFile(sDirList, sCurDir, nIndex, nPageSize, nRestCount, bIsDir);
	errcode = 0;
	
END_GETSDDIRLIST:
	xml.SetDoc("");
	MakeRespXML(pMsgResp, xml, errcode);
	if (errcode == 0){
		xml.AddChildElem("CODE", nListCount);
		xml.AddChildElem("CUR_DIR", sCurDir);
		xml.AddChildElem("DIR_TYPE", !bIsDir);
		xml.AddChildElem("REST", nRestCount);
		xml.IntoElem();
		xml.AddSubDoc(sDirList);
		xml.OutOfElem();
	}
	SendMsg((char*)xml.GetDoc().c_str());
}

void CDswmessagesession::GetSdRecFnDownReq(void* pMsgReq, const char* pMsgResp)
{
	char sFile[256];
	struct stat nstat;
	int errcode = -1;
	CMarkup xml;
	int nFileLen = -1;
	void* pParam;

	xml.SetDoc((const char*)pMsgReq);
	if (!xml.FindChildElem("DSW_REC"))
		goto END_DOWNRECVIDEOREQ;
	xml.IntoElem();
	if (!xml.FindChildElem("FILE"))
		goto END_DOWNRECVIDEOREQ;
	sprintf(sFile, "%s", xml.GetChildData().c_str());
	xml.OutOfElem();

	pParam = (void*)sFile;
	nFileLen = m_pEnv->pOnNotifyFunc(PMSG_GET_RECFILELEN, pParam);
	if (nFileLen > 0)
		errcode = 0;

END_DOWNRECVIDEOREQ:
	xml.SetDoc("");
	MakeRespXML((char*)pMsgResp, xml, errcode);
	if (errcode == 0){
		xml.AddChildElem("FILELEN", nFileLen);
	}
	else
		xml.AddChildElem("FILELEN", 0);
	SendMsg((char*)xml.GetDoc().c_str());
}

void CDswmessagesession::GetSdRecFnPlayReq(void* pMsgReq, const char* pMsgResp)
{
	char sFile[256];
	CMarkup xml;
	void* pParam;
	ST_3GP_PLAY_T n3GPPlay;
	int errcode = -1;

	xml.SetDoc((const char*)pMsgReq);
	if (!xml.FindChildElem("DSW_REMOTE"))
		goto END_PLAYRECVIDEOREQ;
	xml.IntoElem();
	if (!xml.FindChildElem("PUC_ID"))
		goto END_PLAYRECVIDEOREQ;
	xml.OutOfElem();
	if (!xml.FindChildElem("DSW_REC"))
		goto END_PLAYRECVIDEOREQ;
	xml.IntoElem();
	if (!xml.FindChildElem("FILE"))
		goto END_PLAYRECVIDEOREQ;
	sprintf(sFile, "%s", xml.GetChildData().c_str());
	xml.OutOfElem();

	n3GPPlay.pFileName = sFile;
	pParam = (void*)&n3GPPlay;
	if (m_pEnv->pOnNotifyFunc(PMSG_GET_FILERESOLUTION, pParam) == 0)
		errcode = 0;
	else
		memset(&n3GPPlay, 0, sizeof(ST_3GP_PLAY_T));
	
END_PLAYRECVIDEOREQ:
	xml.SetDoc("");
	MakeRespXML((char*)pMsgResp, xml, errcode);
	xml.AddChildElem("DSW_REC");
	xml.IntoElem();
	xml.AddChildElem("FILELEN", n3GPPlay.nFileLen);
	xml.AddChildElem("SCALE", n3GPPlay.nDuration);
	xml.AddChildElem("FPS", n3GPPlay.nFps);
	xml.AddChildElem("WIDTH", n3GPPlay.nWidth);
	xml.AddChildElem("HEIGHT", n3GPPlay.nHeight);
	xml.OutOfElem();
	SendMsg((char*)xml.GetDoc().c_str());
}

void CDswmessagesession::ProcessStopRecVideoReq(void* pMsgReq, const char* pMsgResp)
{
	int errcode = -1;
	CMarkup xml;
	void* pParam;
	ST_PLAYBACK_NODE_T nNode;

	xml.SetDoc((const char*)pMsgReq);
	if (!xml.FindChildElem("DSW_REMOTE"))
		goto END_STOPRECVIDEOREQ;
	xml.IntoElem();
	if (!xml.FindChildElem("PUC_ID"))
		goto END_STOPRECVIDEOREQ;
	sprintf(nNode.sPucid, "%s", xml.GetChildData().c_str());
	xml.OutOfElem();

	nNode.nCmd = EN_PLAY_STOP;
	pParam = (void*)&nNode;
	m_pEnv->pOnNotifyFunc(PMSG_UPDATE_PLAYBACKLIST, pParam);
	errcode = 0;

END_STOPRECVIDEOREQ:
	xml.SetDoc("");
	MakeRespXML((char*)pMsgResp, xml, errcode);
	xml.AddChildElem("DSW_RESULT");
	xml.IntoElem();
	xml.AddChildElem("ErrorCode", errcode);
	m_pEnv->pOnNotifyFunc(PMSG_GET_BASIC, pParam);
	xml.AddChildElem("PUA_ID", ((LPST_DEVICE_BASE)pParam)->devid);
	xml.OutOfElem();
	SendMsg((char*)xml.GetDoc().c_str());
}

void CDswmessagesession::ProcessSetRecVideoSpdReq(void* pMsgReq ,const char* pMsgResp)
{
	int errcode=-1;
	CMarkup xml;
	void* pParam;
	ST_PLAYBACK_NODE_T nNode;

	xml.SetDoc((const char*)pMsgReq);
	if (!xml.FindChildElem("DSW_REMOTE"))
		goto END_SETRECVIDEOSPDREQ;
	xml.IntoElem();
	if (!xml.FindChildElem("PUC_ID"))
		goto END_SETRECVIDEOSPDREQ;
	sprintf(nNode.sPucid, "%s", xml.GetChildData().c_str());
	xml.OutOfElem();
	if (!xml.FindChildElem("DSW_REC"))
		goto END_SETRECVIDEOSPDREQ;
	xml.IntoElem();
	if (!xml.FindChildElem("SPEED"))
		goto END_SETRECVIDEOSPDREQ;
	nNode.nSpeed = atoi(xml.GetChildData().c_str());
	xml.OutOfElem();

	nNode.nCmd = EN_PLAY_SPEED;
	pParam = (void*)&nNode;
	m_pEnv->pOnNotifyFunc(PMSG_UPDATE_PLAYBACKLIST, pParam);
	errcode = 0;

END_SETRECVIDEOSPDREQ:
	xml.SetDoc("");
	MakeRespXML((char*)pMsgResp, xml, errcode);
	xml.AddChildElem("DSW_RESULT");
	xml.IntoElem();
	xml.AddChildElem("ErrorCode", errcode);
	m_pEnv->pOnNotifyFunc(PMSG_GET_BASIC, pParam);
	xml.AddChildElem("PUA_ID", ((LPST_DEVICE_BASE)pParam)->devid);
	xml.OutOfElem();
	SendMsg((char*)xml.GetDoc().c_str());
}

void CDswmessagesession::ProcessSetRecVideoPosReq(void* pMsgReq,const char* pMsgResp)
{
	int errcode=-1;
	CMarkup xml;
	void* pParam;
	ST_PLAYBACK_NODE_T nNode;

	xml.SetDoc((const char*)pMsgReq);
	if (!xml.FindChildElem("DSW_REMOTE"))
		goto END_SETRECVIDEOPOSREQ;
	xml.IntoElem();
	if (!xml.FindChildElem("PUC_ID"))
		goto END_SETRECVIDEOPOSREQ;
	sprintf(nNode.sPucid, "%s", xml.GetChildData().c_str());
	xml.OutOfElem();
	if (!xml.FindChildElem("DSW_REC"))
		goto END_SETRECVIDEOPOSREQ;
	xml.IntoElem();
	if (!xml.FindChildElem("TIME"))
		goto END_SETRECVIDEOPOSREQ;
	nNode.nSeekPos = atoi(xml.GetChildData().c_str());
	xml.OutOfElem();

	nNode.nCmd = EN_PLAY_SEEK;
	pParam = (void*)&nNode;
	m_pEnv->pOnNotifyFunc(PMSG_UPDATE_PLAYBACKLIST, pParam);
	errcode = 0;

END_SETRECVIDEOPOSREQ:
	xml.SetDoc("");
	MakeRespXML((char*)pMsgResp, xml, errcode);
	xml.AddChildElem("DSW_RESULT");
	xml.IntoElem();
	xml.AddChildElem("ErrorCode", errcode);
	m_pEnv->pOnNotifyFunc(PMSG_GET_BASIC, pParam);
	xml.AddChildElem("PUA_ID", ((LPST_DEVICE_BASE)pParam)->devid);
	xml.OutOfElem();
	SendMsg((char*)xml.GetDoc().c_str());
}


